/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.monitor.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.monitor.entity.WorkOrder;
import org.springblade.modules.monitor.dto.WorkOrderDTO;
import org.springblade.modules.monitor.service.IWorkOrderService;
import org.springblade.modules.monitor.vo.WorkOrderVO;
import org.springblade.modules.system.entity.Param;
import org.springblade.modules.system.service.IDictService;
import org.springblade.modules.system.service.IParamService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;

import org.springblade.core.boot.ctrl.BladeController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import static org.springblade.modules.auth.utils.NoticeAlertUtil.*;

/**
 *  控制器
 *
 * @author Blade
 * @since 2020-05-17
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-monitor/workorder")
@Api(value = "工单信息", tags = "工单接口")
public class WorkOrderController extends BladeController {

	private IWorkOrderService workOrderService;

	private IDictService iDictService;

	private IParamService paramService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "工单详情", notes = "传入workOrder")
	public R<WorkOrder> detail(Long workOrderId) {
		WorkOrder workOrder = new WorkOrder();
		workOrder.setWorkOrderId(workOrderId);
		WorkOrder detail = workOrderService.getOne(Condition.getQueryWrapper(workOrder));
		//对故障信息进行转换
		String orderDescribe = detail.getOrderDescribe();
		JSONObject message =  transferMessage(orderDescribe);
		String content = "告警主机: " + message.get("hostName") + "</br>" +
			"设备IP: " + message.get("hostIP") + "</br>" +
			"告警等级: " + transferSeverity(message.get("eventSeverity").toString()) + "</br>" +
			"问题概述: " + message.get("eventName") + "</br>";
		content += "告警时间: " + transferDateTime(message.get("eventTime").toString()) + "</br>";
		detail.setOrderDescribe(content);
		return R.data(detail);
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入workOrder")
	public R<IPage<WorkOrderVO>> list(WorkOrderDTO workOrderDTO, Query query, HttpServletRequest request, HttpServletResponse response) {


		BladeUser user = SecureUtil.getUser(request);
		workOrderDTO.setLoginUserId(user.getUserId());
		LocalDateTime fastEndTime = LocalDateTime.now();

		//查询配置的时间间隔
		List<Param> list = paramService.list();
		Optional<Param> first = list.stream().filter(x -> {
			return x.getParamKey().equalsIgnoreCase("system.workOder.interval");
		}).findFirst();
		Integer interval =  Integer.valueOf(first.get().getParamValue());

		//设置快速查询 开始时间、结束时间
		LocalDateTime fastStartTime = fastEndTime.minusDays(interval);
		workOrderDTO.setFastStartTime(fastStartTime);
		workOrderDTO.setFastEndTime(fastEndTime);


		IPage<WorkOrderVO> pages = workOrderService.selectWorkOrderPage(Condition.getPage(query), workOrderDTO);
		return R.data(pages);
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "工单新增或修改", notes = "传入workOrder")
	public R submit(@Valid @RequestBody WorkOrder workOrder) {
		LocalDateTime updateTime = LocalDateTime.now();
		workOrder.setUpdateTime(updateTime);
		return R.status(workOrderService.saveOrUpdate(workOrder));
	}


	/**
	 * 新增或修改
	 */
	@GetMapping("/deliverToMe")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "将此工单分配给自己", notes = "传入workOrderId")
	public R deliverToMe(Long workOrderId) {
		WorkOrder workOrder = new WorkOrder();
		BladeUser user = SecureUtil.getUser();
		workOrder.setCurrentUserId(user.getUserId());
		workOrder.setWorkOrderId(workOrderId);
		return R.status(workOrderService.saveOrUpdate(workOrder));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(workOrderService.removeByIds(Func.toLongList(ids)));
	}


}
